import AbilityUtil from '@/AbilityUtil';
import ArrayUtil from '@/ArrayUtil';
import BaseUtil from '@/BaseUtil';
import MathUtil from '@/MathUtil';
import RandomUtil from '@/RandomUtil';
import MovementCommon from 'code/Core/Common/MovementCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 刀扇
 **/
export default class DZ_FanKnive extends AbilityBase {
    static instance: DZ_FanKnive;
    constructor(id: string = 'Ads1') {
        super(id);
        DZ_FanKnive.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
    }
    init() {}
    coolPool: unit[] = [];
    missileMap: Map<unit, unit> = new Map();
    knivesEffect(caster: unit) {
        let f = RandomUtil.randomAngle();
        let count = 20;
        let stp = 360 / count;
        let lv = GetUnitAbilityLevel(caster, this.id);
        let dmg = 20 * lv + UnitCommon.取攻击(caster) * (0.4 + 0.3 * lv);
        BaseUtil.onTimer(0.02, (c) => {
            MovementCommon.LineMissile({
                missile: 'Abilities\\Weapons\\WardenMissile\\WardenMissile.mdl',
                caster,
                angle: f + c * stp,
                speed: 600,
                is_through: true,
                radius: 50,
                distance: 400,
                onHit: (p, h) => {
                    DamageSystemInstance.applyPhysicalDamage(h, caster, dmg, this.id);
                },
            });
            return c < count;
        });
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        let cool = 0;
        if (!this.coolPool.includes(source)) {
            this.coolPool.push(source);
            this.knivesEffect(source);
            BaseUtil.runLater(0.1, () => {
                cool = AbilityUtil.getUnitAbilityStateCooldown(source, this.id);
                AbilityUtil.setUnitAbilityStateCooldown(source, this.id, 0);
            });

            BaseUtil.runLater(3, () => {});
            let angle = MathUtil.angleBetweenCoords(GetUnitX(source), GetUnitY(source), tarX, tarY);
            let p = MovementCommon.LineMissile({
                missile: 'Abilities\\Weapons\\WardenMissile\\WardenMissile.mdl',
                distance: 900,
                angle,
                is_through: true,
                caster: source,
                notHit: true,
                speed: 450,
                onFinish: (p) => {
                    if (this.coolPool.includes(source)) {
                        ArrayUtil.removeElement(this.coolPool, source);
                        AbilityUtil.setUnitAbilityStateCooldown(source, this.id, cool - p.curLoop * 0.04);
                    }
                    if (this.missileMap.has(p.caster)) {
                        this.missileMap.delete(p.caster);
                    }
                },
            });
            this.missileMap.set(source, p.missileUnit);
        } else {
            ArrayUtil.removeElement(this.coolPool, source);
            let missile = this.missileMap.get(source);
            this.knivesEffect(source);
            this.missileMap.delete(source);
            let x = GetUnitX(missile);
            let y = GetUnitY(missile);
            SetUnitX(source, x);
            SetUnitY(source, y);
        }
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
}
